package cn.bytecollege.service;

import cn.bytecollege.mapper.*;
import cn.bytecollege.model.OrderDetail;
import cn.bytecollege.model.Orders;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物资配送人员Service
 */
@Service
@Log
public class DiliveryService {
    //创建Mapper实例
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 骑手查询订单的方法
     * @return
     */
    public List<Orders> get(){
        //获取orders表中所有数据并放入orderList集合中
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().eq("order_state","待发货").select("order_id", "user_id", "send_user_id", "order_state", "order_date", "user_address_id", "order_check");
        List<Orders> orderList = orderMapper.selectList(wrapper);
        //遍历orderList集合
        for (Orders orders : orderList) {
            //在orderList中获取userAddressId
            Integer userAddressId = orders.getUserAddressId();
            //根据userAddressId在user_address表中查找数据并放入orderList对象的userAddress中
            orders.setUserAddress(userAddressMapper.selectById(userAddressId));
            //在orderList中获取orderId
            Integer orderId = orders.getOrderId();
            //根据orderId在order_detial表中查找数据并放入orderList对象的orderDetailList集合中
            QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>().eq("order_id", orderId).select("order_detail_id", "order_id", "goods_id", "goods_price", "goods_number");
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(detailWrapper);
            orders.setOrderDetailList(orderDetailList);
            Double sum =0d;
            //遍历orderDetailList
            for (OrderDetail detail : orderDetailList) {
                //获取goodsId
                Integer goodsId = detail.getGoodsId();
                //根据goodsId在goods表中查找数据并放入orderDetail的goods对象中
                detail.setGoods(goodsMapper.selectById(goodsId));
                detail.setSum(detail.getGoodsPrice()*detail.getGoodsNumber());
                sum=sum+detail.getSum();
            }
            orders.setSum(sum);
        }
        return orderList;
    }

    /**
     * 骑手接单的方法
     * @param userId
     * @param orderId
     * @return
     */
    public int mod(Integer userId, Integer orderId){
        //根据ordersId查询出orders表中的订单信息
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().eq("order_id",orderId).select("order_id","user_id","send_user_id","order_state","order_date","user_address_id","order_check");
        Orders orders = orderMapper.selectOne(wrapper);
        //判断订单中的sendUserId是否为空，如果为空
        if(orders.getSendUserId()==null){
            //将sendUserId改成当前骑手的userId
            orders.setSendUserId(userId);
            //将订单状态改成待签收
            orders.setOrderState("待签收");
            //修改orders表信息
            orderMapper.updateById(orders);
            //返回1
            return 1;
        }
        //如果失败返回0
        return 0;
    }

    /**
     * 骑手查看自己接单的订单方法
     * @param userId
     * @return
     */
    public List<Orders> getById(Integer userId){
        //获取orders表中所有数据并放入orderList集合中
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().eq("send_user_id", userId).select("order_id", "user_id", "send_user_id", "order_state", "order_date", "user_address_id", "order_check");
        List<Orders> orderList = orderMapper.selectList(wrapper);
        //遍历orderList集合
        for (Orders orders : orderList) {
            //在orderList中获取userAddressId
            Integer userAddressId = orders.getUserAddressId();
            //根据userAddressId在user_address表中查找数据并放入orderList对象的userAddress中
            orders.setUserAddress(userAddressMapper.selectById(userAddressId));
            //在orderList中获取orderId
            Integer orderId = orders.getOrderId();
            //根据orderId在order_detial表中查找数据并放入orderList对象的orderDetailList集合中
            QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<OrderDetail>().eq("order_id", orderId).select("order_detail_id", "order_id", "goods_id", "goods_price", "goods_number");
            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(detailWrapper);
            orders.setOrderDetailList(orderDetailList);
            Double sum =0d;
            //遍历orderDetailList
            for (OrderDetail detail : orderDetailList) {
                //获取goodsId
                Integer goodsId = detail.getGoodsId();
                //根据goodsId在goods表中查找数据并放入orderDetail的goods对象中
                detail.setGoods(goodsMapper.selectById(goodsId));
                detail.setSum(detail.getGoodsPrice()*detail.getGoodsNumber());
                sum=sum+detail.getSum();
            }
            orders.setSum(sum);
        }
        return orderList;
    }

    public int delOrderById(Integer orderId){
        //根据ordersId查询出orders表中的订单信息
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>().eq("order_id",orderId).select("order_id","user_id","send_user_id","order_state","order_date","user_address_id","order_check");
        Orders orders = orderMapper.selectOne(wrapper);
        if (orders.getOrderState().equals("待评价")||orders.getOrderState().equals("评价完成")||orders.getOrderState().equals("已取消")){
            orderMapper.updateSendIdById(orders);
            return 1;
        }
        return 0;
    }

}
